﻿using BLL;
using Model;
using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Thksoft.UserCotrols
{
    public partial class CompanyControl : System.Web.UI.Page
    {
        private Sys_CompanyBLL cbll = new Sys_CompanyBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (HttpContext.Current.Session["SysUserInfo"] != null)
                {
                    string companyId = ((Sys_User)(HttpContext.Current.Session["SysUserInfo"])).USER_COMPANY.ToString();
                    this.hidCompanyId.Value = companyId;
                    Com_Name();
                }
                else
                {
                    var msg = "未登录或者登录已失效!";
                    StringBuilder Builder = new StringBuilder();
                    Builder.AppendLine("<script type=\"text/javascript\">");
                    Builder.AppendLine("    if(typeof top.MessageBox != \"undefined\"){");
                    Builder.AppendLine("        var option={");
                    Builder.AppendLine("                Msg:\"" + msg + "\",");
                    Builder.AppendLine("                YesCallBack:function(index){");
                    Builder.AppendLine("                    top.MessageBox.RedirectLogin();");
                    Builder.AppendLine("                },");
                    Builder.AppendLine("                options:{");
                    Builder.AppendLine("                    success:function(layero,index){");//}");
                    //Builder.AppendLine("                    console.log(\"erro\");");
                    Builder.AppendLine("                        setTimeout(");
                    Builder.AppendLine("                            \"Go();\",");
                    Builder.AppendLine("                            30000);");
                    Builder.AppendLine("                    }");
                    Builder.AppendLine("                }");
                    Builder.AppendLine("        };");
                    Builder.AppendLine("        function Go(){");
                    Builder.AppendLine("            top.MessageBox.RedirectLogin();");
                    Builder.AppendLine("        }");
                    Builder.AppendLine("        top.MessageBox.Alert(option);");
                    Builder.AppendLine("    }");
                    Builder.AppendLine("    else{");
                    Builder.AppendLine("        alert(\"" + msg + "\");");
                    Builder.AppendLine("        location.href=\"/Login.aspx\";");
                    Builder.AppendLine("    }");
                    Builder.AppendLine("</script>");

                    Response.Write(Builder.ToString());
                    Response.End();
                }
            }

            this.hidCompanyId.Value = Request.QueryString["comId"].ToString();
        }

        public void Com_Name()
        {
            //公司名称

            DataTable dtCompany = new DataTable();

            if (!String.IsNullOrEmpty(hidCompanyId.Value))
            {
                dtCompany = cbll.GetInfoByPId(Convert.ToInt32(hidCompanyId.Value));
                hidPCompanyId.Value = dtCompany.Rows[0]["CMY_PARENTID"].ToString();
            }
            else
            {
                //dtCompany = cbll.GetAllInfo();
                dtCompany = cbll.GetInfoByPId(Convert.ToInt32(hidCompanyId.Value));
                hidPCompanyId.Value = dtCompany.Rows[0]["CMY_PARENTID"].ToString();
            }
            if (dtCompany != null)
            {
                CreateCompanyTree(dtCompany, decimal.Parse(hidPCompanyId.Value), tvProcessUsers, null);
            }
        }

        /// <summary>
        /// 定义一个方法 这个方法是用来创建公司节点的方法
        /// </summary>
        /// <param name="dtCompany">传入的是一个公司的dt对象</param>
        /// <param name="dtDepartment">传入的是一个节点的dt对象</param>
        /// <param name="dtUser">传入的是一个用户的dt对象</param>
        /// <param name="parentID">传入的是一个父节点的ID</param>
        /// <param name="tvProcessUsers">传入的一个TreeView对象</param>
        /// <param name="pNode">传入的i\yige节点ID</param>
        protected void CreateCompanyTree(DataTable dtCompany, decimal parentID, TreeView tvProcessUsers, TreeNode pNode)
        {
            DataRow[] row = dtCompany.Select(" CMY_PARENTID=" + parentID + "");
            for (int i = 0; i < row.Length; i++)
            {
                TreeNode node = new TreeNode();
                if (pNode == null)
                {
                    //绑定节点的值，显示节点名称
                    node.Text = row[i]["CMY_NAME"].ToString() + "【公司】";
                    //给节点显示的名称绑定一个值，这个值为该名称的ID
                    node.Value = row[i]["CMY_ID"].ToString();
                    node.ToolTip = "公司";
                    //设置这个节点是否张开复选框
                    node.ShowCheckBox = true;
                    //设置这个节点是否展开
                    node.Expanded = true;
                    //根据ID得到的节点添加到对应的父节点下面
                    tvProcessUsers.Nodes.Add(node);
                    //用递归来循环创建公司节点
                    CreateCompanyTree(dtCompany, Convert.ToDecimal(row[i]["CMY_ID"].ToString()), tvProcessUsers, node);
                }
                else
                {
                    //绑定节点的值，显示节点名称
                    node.Text = row[i]["CMY_NAME"].ToString() + "【公司】";
                    //给节点显示的名称绑定一个值，这个值为该名称的ID
                    node.Value = row[i]["CMY_ID"].ToString();
                    node.ToolTip = "公司";
                    //判断是否展开复选框
                    node.ShowCheckBox = true;
                    //判断是否展开节点
                    node.Expanded = true;
                    //根据ID得到的节点添加到对应的父节点下面
                    pNode.ChildNodes.Add(node);
                    //运用递归循环创建公司
                    CreateCompanyTree(dtCompany, Convert.ToDecimal(row[i]["CMY_ID"].ToString()), tvProcessUsers, node);
                }
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string name = string.Empty;

            //实例化一个StringBuilder 来存储公司ID
            StringBuilder inID = new StringBuilder();

            //得到TreeView对象
            TreeNodeCollection nodes = tvProcessUsers.CheckedNodes;
            int j = 1;

            for (int i = 0; i < nodes.Count; i++)
            {
                //判断选择的是不是一个公司
                if (nodes[i].ToolTip == "公司")
                {
                    if (j == 1)
                    {
                        inID.Append(nodes[i].Value);
                        name = nodes[i].Text.Split('【')[0].ToString();
                        name = name.Replace("<font color='Red'>", "'");
                        name = name + "'";
                    }
                    else
                    {
                        name = name + ",";
                        name = name + nodes[i].Text.Split('【')[0].ToString();
                        name = name.Replace("<font color='Red'>", "'");
                        name = name + "'";
                        inID.Append(",");
                        inID.Append(nodes[i].Value);
                    }
                    j++;
                }
            }

            //没有选择公司
            if (j == 1)
            {
                MessageBox.Show(this, "【系统提示】请选择公司！");
                return;
            }

            if (Request["check"] != null)
            {
                if (Request["check"].ToString() == "true" && j > 2)
                {
                    MessageBox.Show(this, "【系统提示】你只能选择一个公司！");
                    return;
                }
            }

            //关闭当前窗口 出现的错是 为定义 参数无效 不支持此熟悉和方法
            name = name.Replace("'", "");

            name += "%" + inID;

            MessageBox.ResponseScript(this, "companyGo('" + name + "');");
        }
    }
}